#ifndef A8_ATOMIC_H
#define A8_ATOMIC_H

#if defined(__linux__) || defined(__FREEBSD__)

namespace a8
{
  static inline void AtomicAddLong(volatile unsigned long *p, unsigned long v)
  {
    __asm __volatile("lock ; add l %1,%0"
		     : "=m" (*p)
		     : "ir" (v), "m" (*p));
  }

  static inline void AtomicSubLong(volatile unsigned long *p, unsigned long v)
  {
    __asm __volatile(" lock ; subl %1,%0"
		     : "=m" (*p)
		     : "ir" (v), "m" (*p));
  }
}

#define InterlockedIncrement(inc) a8::AtomicAddLong(inc, 1)
#define InterlockedDecrement(dec) a8::AtomicDecLong(dec, 1)

#endif

#endif
